# _*_ coding : utf-8 _*_
# @Time : 2024-06-05 12:53
# @Author : haowen
# @File : sharesChats
# @Project : pyserver

import akshare as ak
from pyecharts.charts import Line, Kline, Bar
from pyecharts import options as opts

def createChars(startData,sharesCode):
    # 获取股票数据
    # "20240430"
    start_date = startData
    # "000004"
    symbol = sharesCode
    stock_zh_a_hist_df = ak.stock_zh_a_hist(
        symbol=symbol,
        period="daily",
        start_date=start_date,
        adjust="hfq"
    )

    # 计算均线（MA）
    stock_zh_a_hist_df["ma5"] = stock_zh_a_hist_df["收盘"].rolling(window=5).mean()
    stock_zh_a_hist_df["ma10"] = stock_zh_a_hist_df["收盘"].rolling(window=10).mean()

    # 计算布林线（BOLL）
    std = stock_zh_a_hist_df["收盘"].rolling(window=20).std()
    stock_zh_a_hist_df["middleband"] = stock_zh_a_hist_df["收盘"].rolling(window=20).mean()
    stock_zh_a_hist_df["upperband"] = stock_zh_a_hist_df["middleband"] + 2 * std
    stock_zh_a_hist_df["lowerband"] = stock_zh_a_hist_df["middleband"] - 2 * std

    # 计算KDJ指标
    k_line = stock_zh_a_hist_df["最高"].tolist()
    l_line = stock_zh_a_hist_df["最低"].tolist()
    c_line = stock_zh_a_hist_df["收盘"].tolist()

    n = 9
    rsv_list = []
    k_list = []
    d_list = []

    for i in range(len(c_line)):
        if i < n:
            rsv_list.append(0)
        else:
            hn = max(k_line[i - n:i])
            ln = min(l_line[i - n:i])
            rsv = (c_line[i] - ln) / (hn - ln) * 100
            rsv_list.append(rsv)

    for i in range(len(rsv_list)):
        if i == 0:
            k_list.append(50)
            d_list.append(50)
        else:
            k = (2 / 3) * k_list[i - 1] + (1 / 3) * rsv_list[i]
            d = (2 / 3) * d_list[i - 1] + (1 / 3) * k
            k_list.append(k)
            d_list.append(d)

    # 计算MACD指标
    short_ma = stock_zh_a_hist_df["收盘"].ewm(span=12, min_periods=0, adjust=False).mean()
    long_ma = stock_zh_a_hist_df["收盘"].ewm(span=26, min_periods=0, adjust=False).mean()
    macd = short_ma - long_ma
    signal_line = macd.ewm(span=9, min_periods=0, adjust=False).mean()
    macd_hist = macd - signal_line

    # 绘制均线和布林线图表
    line = Line()
    line.add_xaxis(stock_zh_a_hist_df["日期"].tolist())
    line.add_yaxis("Close Price", stock_zh_a_hist_df["收盘"].tolist())
    line.add_yaxis("MA5", stock_zh_a_hist_df["ma5"].tolist())
    line.add_yaxis("MA10", stock_zh_a_hist_df["ma10"].tolist())
    line.set_global_opts(title_opts=opts.TitleOpts(title="均线"))

    kline = Kline()
    kline.add_xaxis(stock_zh_a_hist_df["日期"].tolist())
    kline.add_yaxis("Kline", stock_zh_a_hist_df[["开盘", "收盘", "最低", "最高"]].values.tolist())
    kline.set_global_opts(title_opts=opts.TitleOpts(title="布林线"))

    # 绘制KDJ指标图表
    line_k = Line()
    line_k.add_xaxis(stock_zh_a_hist_df["日期"].tolist())
    line_k.add_yaxis("K", k_list)
    line_k.add_yaxis("D", d_list)
    line_k.set_global_opts(title_opts=opts.TitleOpts(title="KDJ指标"))

    # 绘制MACD指标图表
    bar_macd = Bar()
    bar_macd.add_xaxis(stock_zh_a_hist_df["日期"].tolist())
    bar_macd.add_yaxis("MACD", macd.tolist(), label_opts=opts.LabelOpts(is_show=False))
    bar_macd.add_yaxis("Signal Line", signal_line.tolist(), label_opts=opts.LabelOpts(is_show=False))
    bar_macd.set_global_opts(title_opts=opts.TitleOpts(title="MACD指标"))

    #  使用api时候 用 ../
    # 在本此处运行时候用../../
    line.render("../public/macd.htmljunxian.html")
    kline.render("../public/bulinxian.html")
    line_k.render("../public/kdj.html")
    bar_macd.render("../public/macd.html")

# createChars("20240430","000004")